package com.admin_springboot.adminspringboot.controller;

import com.admin_springboot.adminspringboot.common.Result;
import com.admin_springboot.adminspringboot.entity.User;
import com.admin_springboot.adminspringboot.mapper.UserMapper;
import com.admin_springboot.adminspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    //demo
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;


    @GetMapping ("/all")
    public Result getUser(){
        return Result.success("查询成功",userMapper.findAll());
    }

    @GetMapping ("/page")
    public Result getUserByPage(@RequestParam Integer pageNum , @RequestParam Integer pageSize, String username, String phone){

        Map res=userService.selectByPage(pageNum,pageSize,username,phone);
        return Result.success("查询成功",res);
    }

    @PostMapping
    public Result save(@RequestBody User user){
        Integer res=userService.saveUser(user);
        if(res==1){
            return Result.success();
        }
        return Result.error("新增失败");
    }
    @PutMapping
    public Result update(@RequestBody User user){
        Integer res= userService.editUSer(user);
        if(res==1){
            return Result.success();
        }
        return Result.error("更新失败");
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        Integer res = userService.deleteUser(id);
        if(res==1){
            return Result.success();
        }
        return Result.error("删除失败");
    }
}
